
package com.example.gadgeteer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Html;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TableLayout.LayoutParams;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener
{
	/* request BT enable */
	private static final int  REQUEST_ENABLE      = 0x1;
	/* request BT discover */
	private static final int  REQUEST_DISCOVERABLE  = 0x2;
	  
	private LinearLayout linearLayout1;
	BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
	private BluetoothSocket btSocket = null;
	private Button enable;
	private Button disable;
	private Button client;
	private Button client2;
	private Button status;
	private Button discovery;
	private Button server;
	private Button serverService;
	private Button location;
	private Button stopServices;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		createLayout(); 
		setContentView(linearLayout1);
	}

	private void createLayout()
	{
		linearLayout1 = new LinearLayout(this);
		linearLayout1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));  
		linearLayout1.setOrientation(LinearLayout.VERTICAL);
		linearLayout1.setBackgroundColor(Color.WHITE);
		linearLayout1.setId(1000);
		linearLayout1.setBackgroundColor(Color.LTGRAY);
		//
		TextView label = new TextView(this);
		label.setBackgroundColor(Color.BLACK);
		label.setTextColor(Color.WHITE);
		label.setTextSize(14);
		label.setText("Find my Device prototype");
		label.setPadding(6, 5, 6, 5);
		label.setGravity(Gravity.CENTER_HORIZONTAL);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		lp.setMargins(0, 0, 0, 10);
		linearLayout1.addView(label,lp);
		//
		enable = new Button(this);
		enable.setText("Enable Blutooth");
		enable.setId(1);
		enable.setOnClickListener(this);
		linearLayout1.addView(enable);
		//
		disable = new Button(this);
		disable.setText("Disable Blutooth");
		disable.setId(2);
		disable.setOnClickListener(this);
		linearLayout1.addView(disable);
		//
		client = new Button(this);
		client.setText("client");
		client.setId(3);
		client.setOnClickListener(this);
		//linearLayout1.addView(client);
		//
		client2 = new Button(this);
		client2.setText("client2");
		client2.setId(6);
		client2.setOnClickListener(this);
		//linearLayout1.addView(client2);
		//
		status = new Button(this);
		status.setText("status");
		status.setId(4);
		status.setOnClickListener(this);
		linearLayout1.addView(status);
		//
		discovery = new Button(this);
		discovery.setText("discovery");
		discovery.setId(5);
		discovery.setOnClickListener(this);
		linearLayout1.addView(discovery);
		//
		server = new Button(this);
		server.setText("server");
		server.setId(7);
		server.setOnClickListener(this);
		//linearLayout1.addView(server);
		//
		serverService = new Button(this);
		serverService.setText("server service");
		serverService.setId(8);
		serverService.setOnClickListener(this);
		linearLayout1.addView(serverService);
		//
		location = new Button(this);
		location.setText("location service");
		location.setId(9);
		location.setOnClickListener(this);
		linearLayout1.addView(location);
		//stopServices
		stopServices = new Button(this);
		stopServices.setText("Stop Services");
		stopServices.setId(10);
		stopServices.setOnClickListener(this);
		linearLayout1.addView(stopServices);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
	      case 1:
	      	bluetooth.enable();
	         Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
	         startActivityForResult(enabler, REQUEST_DISCOVERABLE);
	      break;
	      case 2:
	      	bluetooth.disable();
	      	break;
		    //
	      case 3:
	   		Intent intent = new Intent(this, ClientSocketActivity.class);
	   		startActivity(intent);
	      	break;
	      case 4:
	      	String status;
	      	if (bluetooth.isEnabled()) {
	      	    String mydeviceaddress = bluetooth.getAddress();
	      	    String mydevicename = bluetooth.getName();
	      	    int state = bluetooth.getState();
	      	    status = mydevicename + " : " + mydeviceaddress + " , " + state;
	      	  
	      	}
	      	else
	      	{
	      	    status = "Bluetooth is not Enabled.";
	      	}
	      	Toast.makeText(this, status, Toast.LENGTH_LONG).show();
	      	break;
	      case 5:
	      	if (bluetooth.isEnabled()) {
	   		Intent intent2 = new Intent(this, DiscoveryActivity.class);
	   		startActivity(intent2);
	      	}
	      	else {
	      		Toast.makeText(this, "Bluetooth is not Enabled", Toast.LENGTH_LONG).show();
	      	}
	      	break;
	      case 6:
	   		Intent intent3 = new Intent(this, ClientSocketActivity2.class);
	   		startActivity(intent3);
	      	break;
	      case 7:
	   		Intent intent4 = new Intent(this, ServerSocketActivity.class);
	   		startActivity(intent4);
	      	break;
	      case 8:
	      	Thread t = new Thread(){
	      		@Override
					public void run(){
	      		startService(new Intent(getApplicationContext(),ServerSocketService.class));
	      		}
	      	};
	      		t.start();
	      	break;
	      case 9:
	      	Thread tt = new Thread(){
	      		@Override
					public void run(){
	      		startService(new Intent(getApplicationContext(),LocationService.class));
	      		}
	      	};
	      		tt.start();
	      	break;
	      case 10:
	      	stopService(new Intent(getApplicationContext(), LocationService.class));
	      	break;
		}
	}
}
